<template>
    <BasePage>
        <el-form slot="form" :inline="true" size="medium" @submit.native.prevent>
            <el-form-item label="管养单位">
                <CustodyOrgSelect :value.sync="form.orgName" :is-label="true" clearable></CustodyOrgSelect>
            </el-form-item>
            <el-form-item label="决策项目名称">
                <el-input v-model.trim="form.decisionItemName" clearable placeholder="请输入" />
            </el-form-item>
            <el-form-item label="创建年份">
                <el-date-picker v-model="form.createYear" type="year" format="yyyy" value-format="yyyy" :clearable="false" placeholder="请选择">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <BaseButton v-permission="{ btnCode: 'dlyh_c51_d233_2' }" @click="handleFilter">查询</BaseButton>
                <BaseButton v-permission="{ btnCode: 'dlyh_c51_d233_2' }" type="second" @click="resetConditions">重置</BaseButton>
            </el-form-item>
        </el-form>

        <div slot="button">
            <BaseButton v-permission="{ btnCode: 'dlyh_c51_d230_2' }" @click="handleAdd">新增</BaseButton>
            <BaseButton v-permission="{ btnCode: 'dlyh_c51_d231_2' }" style="margin-right: 8px" :disabled="selectionList.length === 0" type="second" @click="handleDelete">
                删除
            </BaseButton>
        </div>

        <!--数据列表区-->
        <BaseTable
            slot="table"
            v-loading="listLoading"
            :columns="columns"
            :data="list"
            row-key="id"
            height="calc(100% - 73px)"
            highlight-current-row
            @selection-change="handleSelectionChange"
        >
            <template #roadName="{ scope: { row } }">
                {{ row.highwayName }}<span v-if="row.highwayName&&row.roadSectionDetail">{{ '——' }}</span>{{ row.roadSectionDetail }}
            </template>
            <template #handle="{ scope: { row } }">
                <BaseButton v-permission="{ btnCode: 'dlyh_c51_d232_2' }" type="text" @click="handleEdit(row.id)">修改</BaseButton>
            </template>
        </BaseTable>
        <!--分页组件-->
        <Pagination
            slot="pagination"
            :current-page.sync="page.pageNo"
            :page-size.sync="page.pageSize"
            :total="page.total"
            @current-change="getList"
            @size-change="getList"
        />
        <SaveOrUpdate ref="dialog" @success="handleFilter"></SaveOrUpdate>
    </BasePage>
</template>

<script>
import BasePage from '@/layouts/BasePage'
import ExcelImport from '@/components/ExcelImport'
import reconfirm from '@/utils/reconfirm'
import { DecisionItemModel } from '@/models'
import KeepAliveHandler from '@/mixins/keepAliveHandler'
import { DirectionOptions } from '@/config/constants'
import SaveOrUpdate from './SaveOrUpdate'

export default {
    name: 'MaintenanceRecord',
    components: {
        BasePage,
        ExcelImport,
        SaveOrUpdate
    },
    mixins: [
        KeepAliveHandler({
            defaultCondition: {
                orgName: '',
                decisionItemName: '',
                createYear: ''
            },
            conditionObjKey: 'form'
        })
    ],
    data() {
        return {
            DirectionOptions,
            downloadLoading: false,
            templateLoading: false,
            model: new DecisionItemModel(),
            form: {
                orgName: '',
                decisionItemName: '',
                createYear: ''
            },
            listLoading: true,
            list: [],
            page: {
                pageNo: 1,
                pageSize: 10,
                total: 0
            },
            columns: Object.freeze([
                {
                    attrs: {
                        type: 'selection'
                    }
                },
                {
                    attrs: {
                        prop: 'orgName',
                        label: '管养单位',
                        width: '140',
                        'show-overflow-tooltip': true
                    }
                },
                {
                    slot: 'roadName',
                    attrs: {
                        prop: 'roadName',
                        label: '路段名称',
                        'show-overflow-tooltip': true,
                        'min-width': '150'
                    }
                },
                {
                    attrs: {
                        prop: 'decisionItemName',
                        label: '决策项目名称',
                        'show-overflow-tooltip': true,
                        'min-width': '150'
                    }
                },
                {
                    attrs: {
                        prop: 'belongModel',
                        label: '所属模型',
                        'show-overflow-tooltip': true,
                        'min-width': '120'
                    }
                },
                {
                    attrs: {
                        prop: 'maintSchemeName',
                        label: '养护方案',
                        'show-overflow-tooltip': true,
                        width: '130'
                    }
                },
                {
                    attrs: {
                        prop: 'createUserName',
                        label: '创建人',
                        'show-overflow-tooltip': true,
                        width: '110'
                    }
                },
                {
                    attrs: {
                        prop: 'createTime',
                        label: '创建时间',
                        'show-overflow-tooltip': true,
                        width: '170'
                    }
                },
                {
                    attrs: {
                        prop: 'remark',
                        label: '备注',
                        'show-overflow-tooltip': true,
                        'min-width': '140'
                    }
                },
                {
                    slot: 'handle',
                    attrs: {
                        label: '操作',
                        width: '150',
                        fixed: 'right'
                    }
                }
            ]),
            selectionList: []
        }
    },
    created() {
        this.getList()
    },
    methods: {
        getList() {
            this.listLoading = true
            this.model
                .getList(this.form, this.page)
                .then((res) => {
                    this.list = res.list
                    this.page.total = res.totalCount
                })
                .finally(() => {
                    this.listLoading = false
                })
        },
        // 查询
        handleFilter() {
            this.page.pageNo = 1
            this.getList()
        },
        handleAdd() {
            this.$refs['dialog'].open({ type: 'add' })
        },
        handleEdit(id) {
            this.$refs['dialog'].open({ id, type: 'edit' })
        },
        handleDelete() {
            reconfirm({
                text: '是否删除当前数据',
                callback: () => {
                    const ids = this.selectionList.map((item) => item.id)
                    return this.model.delete({ ids }).then(() => {
                        this.getList(true)
                        this.$message.success('删除成功!')
                    })
                }
            })
        },
        handleSelectionChange(value) {
            this.selectionList = value
        }
    }
}
</script>

<style lang="scss" scoped>
.filter-stake {
    width: 203px;
    ::v-deep {
        .el-input__inner,
        .el-input {
            min-width: auto !important;
            line-height: 1px !important;
        }
        .el-input-group__append,
        .el-input-group__prepend {
            padding: 0 8px;
            color: #44444f;
        }
    }
    .start-stake {
        position: relative;
        z-index: 1;
        margin-right: -1px;
        ::v-deep {
            .el-input__inner,
            .el-input {
                border-radius: 0;
            }
        }
    }
    .end-stake {
        ::v-deep {
            .el-input-group__append,
            .el-input-group__prepend {
                border-radius: 0;
            }
        }
    }
}
</style>
